#include "CDirModel.h"
#include <QDebug>

CDirModel::CDirModel(QObject *parent)
    : QAbstractListModel{parent}
{

}

QVariant CDirModel::data(const QModelIndex &index, int role) const
{
    switch (role) {
    case FileInfoRole::fileName:
        return QVariant::fromValue(m_fileInfoList[index.row()].fileName());
    case FileInfoRole::filePath:
        return QVariant::fromValue(m_fileInfoList[index.row()].filePath());
    case FileInfoRole::size:
        return QVariant::fromValue(m_fileInfoList[index.row()].size());
    default:
        return QVariant();
    }
}

int CDirModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_fileInfoList.count();
}

QHash<int, QByteArray> CDirModel::roleNames() const
{
    QHash<int, QByteArray> hash;
    hash[FileInfoRole::fileName] = "fileName";
    hash[FileInfoRole::filePath] = "filePath";
    hash[FileInfoRole::size] = "size";

    return hash;
}

void CDirModel::setPath(QString path)
{
    m_path = path;
    QDir dir(m_path);
    if(!dir.exists()){
        m_fileInfoList.clear();
        return ;
    }
    m_fileInfoList = dir.entryInfoList(QDir::Files);
}
